﻿using System.Collections.Generic;
using System.Data;

namespace Actuals.Data
{
    public abstract class Dal<I, T> where T : I, new()
    {
        protected IEnumerable<I> ConvertAll(DataSet dataSet)
        {
            foreach (DataTable dt in dataSet.Tables)
                foreach (DataRow dr in dt.Rows)
                    yield return Convert(dr);
        }

        protected void Convert(I obj, DataSet dataSet)
        {
            DataTableCollection tables = dataSet.Tables;
            if (tables != null && tables.Count > 0)
            {
                DataRowCollection rows = tables[0].Rows;
                if (rows != null && rows.Count > 0)
                    Convert(obj, dataSet.Tables[0].Rows[0]);
            }
        }

        protected I Convert(DataSet dataSet)
        {
            I obj = new T();
            Convert(obj, dataSet);
            return obj;
        }

        protected I Convert(DataRow row)
        {
            I obj = new T();
            Convert(obj, row);
            return obj;
        }

        protected abstract void Convert(I obj, DataRow row);
    }

}
